package com.lovo.project.service.impl;
import com.lovo.project.dao.ShopDAO;
import com.lovo.project.enums.ShopEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.ShopDO;
import com.lovo.project.service.ShopService;
import com.lovo.project.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import java.util.List;

/**
 * @author 955
 * @title 门店业务实现类
 * @date 2020/11/3 20:25
 */
public class ShopServiceImpl implements ShopService {
    @Override
    public void insertShop(ShopDO shop) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        ShopDAO dao=sqlSession.getMapper(ShopDAO.class);
        try{
            int isInsert=dao.insertShop(shop);
            if(isInsert<=0){
                throw new OurException(ShopEnum.SHOP_INSERTEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public void deleteShop(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        ShopDAO dao=sqlSession.getMapper(ShopDAO.class);
        try{
            int isInsert=dao.deleteShop(id);
            if(isInsert<=0){
                throw new OurException(ShopEnum.SHOP_INSERTEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public ShopDO selectById(Integer id) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        ShopDAO dao=sqlSession.getMapper(ShopDAO.class);
        try{
            ShopDO shop=dao.selectById(id);
            if(shop==null){
                throw new OurException(ShopEnum.SHOP_FINDEXP_ENUM);
            }
            return shop;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }
    }

    @Override
    public List<ShopDO> selectAll(String type, String value) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        ShopDAO dao=sqlSession.getMapper(ShopDAO.class);
        try{
           List<ShopDO>list=dao.selectAll(type,value);
            if(list==null){
                throw new OurException(ShopEnum.SHOP_FINDEXP_ENUM);
            }
            return list;
        }catch (OurException o){
            throw o;
        }finally {
            sqlSession.close();
        }

    }

    @Override
    public void update(ShopDO shop) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        ShopDAO dao=sqlSession.getMapper(ShopDAO.class);
        try{
            int isUpdate=dao.update(shop);
            if(isUpdate<=0){
                throw new OurException(ShopEnum.SHOP_UPDATEEXP_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }
    @Override
    public void updateAudit(ShopDO shop) {
        SqlSession sqlSession= MyBatisUtil.getSqlSession();
        ShopDAO dao=sqlSession.getMapper(ShopDAO.class);
        try{
            int isUpdate=dao.updateAudit(shop);
            if(isUpdate<=0){
                throw new OurException(ShopEnum.SHOP_UPDATEEXP1_ENUM);
            }
            sqlSession.commit();
        }catch (OurException o){
            sqlSession.rollback();
            throw o;
        }finally {
            sqlSession.close();
        }
    }
}
